package org.fu.crowd.service.impl;

import org.fu.crowd.mapper.*;
import org.fu.crowd.po.MemberConfirmInfoPO;
import org.fu.crowd.po.MemberLaunchInfoPO;
import org.fu.crowd.po.ProjectPO;
import org.fu.crowd.po.ReturnPO;
import org.fu.crowd.service.api.ProjectProviderService;
import org.fu.crowd.vo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Transactional(readOnly = true)
@Service
public class ProjectProviderImpl implements ProjectProviderService {

    @Resource
    private ProjectPOMapper projectPOMapper;

    @Resource
    private ProjectItemPicPOMapper projectItemPicPOMapper;

    @Resource
    private MemberLaunchInfoPOMapper memberLaunchInfoPOMapper;

    @Resource
    private MemberConfirmInfoPOMapper memberConfirmInfoPOMapper;

    @Resource
    private ReturnPOMapper returnPOMapper;



    /**
     * 保存projectVO
     * @param projectVO
     * @param memberId
     * @return
     */
    @Transactional(readOnly = false,propagation = Propagation.REQUIRES_NEW,rollbackFor = Exception.class)
    @Override
    public void saveProject(ProjectVO projectVO, Integer memberId) {
        //创建projectPO对象，并将ProjectVO中对应的projectPO对象复制到projectPO
        ProjectPO projectPO = new ProjectPO();
        BeanUtils.copyProperties(projectVO,projectPO);
        projectPO.setMemberid(memberId);
        //创建时间
        String createdate = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        projectPO.setCreatedate(createdate);
        projectPO.setStatus(0);
        //保存，并获得自增后的主键值
        projectPOMapper.insertSelective(projectPO);
        Integer projectPOId = projectPO.getId();

        //保存项目，分类的关联关系信息
        List<Integer> typeIdList = projectVO.getTypeIdList();
        projectPOMapper.insertTypeRelationship(typeIdList,projectPOId);

        //保存项目、标签的关联关系信息
        List<Integer> tagIdList = projectVO.getTagIdList();
        projectPOMapper.insertTagRelationship(tagIdList,projectPOId);

        //保存项目中详情图片的信息
        List<String> detailPicturePathList = projectVO.getDetailPicturePathList();
        projectItemPicPOMapper.insertPathList(projectPOId,detailPicturePathList);

        //保存项目发起人信息
        MemberLauchInfoVO memberLauchInfoVO = projectVO.getMemberLauchInfoVO();
        MemberLaunchInfoPO memberLaunchInfoPO = new MemberLaunchInfoPO();
        BeanUtils.copyProperties(memberLauchInfoVO,memberLaunchInfoPO);
        memberLaunchInfoPO.setMemberid(memberId);
        memberLaunchInfoPOMapper.insert(memberLaunchInfoPO);

        //保存回报信息
        List<ReturnVO> returnVOList = projectVO.getReturnVOList();
        List<ReturnPO> returnPOList = new ArrayList<>();
        for (ReturnVO returnVo : returnVOList){
            ReturnPO returnPO = new ReturnPO();
            BeanUtils.copyProperties(returnVo,returnPO);
            returnPOList.add(returnPO);
        }
        returnPOMapper.insertReturnPOBatch(returnPOList,projectPOId);

        //保存确认信息
        MemberConfirmInfoVO memberConfirmInfoVO = projectVO.getMemberConfirmInfoVO();
        MemberConfirmInfoPO memberConfirmInfoPO = new MemberConfirmInfoPO();
        BeanUtils.copyProperties(memberConfirmInfoVO,memberConfirmInfoPO);
        memberConfirmInfoPO.setMemberid(memberId);
        memberConfirmInfoPOMapper.insert(memberConfirmInfoPO);
    }




    /**
     * 首页信息
     * @return
     */
    @Override
    public List<PortalTypeVO> selectPortalTypeVOList() {
        return projectPOMapper.selectPortalTypeVOList();
    }




    /**
     *  展示给前台的项目详情
     */
    @Override
    public DetailProjectVO getDetailProjectVO(Integer projectId) {
        //获取信息后判断众筹状态
        DetailProjectVO detailProjectVO = projectPOMapper.selectDetailProjectVO(projectId);
        Integer status = detailProjectVO.getStatus();
        switch (status){
            case 0 :
                detailProjectVO.setStatusText("审核中");
                break;
            case 1 :
                detailProjectVO.setStatusText("众筹中");
                break;
            case 2 :
                detailProjectVO.setStatusText("众筹成功");
                break;
            case 3 :
                detailProjectVO.setStatusText("众筹失败");
                break;
            default:
                break;
        }
        //计算lastDay
        //获取发起时间
        String deployDate = detailProjectVO.getDeployDate();
        System.out.println(deployDate);
        String[] splitDeployDate = deployDate.split("-");
        System.out.println(Arrays.toString(splitDeployDate));
        LocalDateTime date = LocalDateTime.of(Integer.parseInt(splitDeployDate[0]),Integer.parseInt(splitDeployDate[1]),Integer.parseInt(splitDeployDate[2]),0,0);
        System.out.println(date);
        //获取众筹天数
        Integer day = detailProjectVO.getDay();
        //计算出结束日期
        LocalDateTime endDate = date.plusDays(day);
        System.out.println(endDate);
        //现在的日期
        LocalDateTime nowDate = LocalDateTime.now();
        System.out.println(nowDate);
        //计算剩余时间
        Duration between = Duration.between(nowDate, endDate);
        Integer endDay = Math.toIntExact(between.toDays());
        detailProjectVO.setLastDay(endDay);

        return detailProjectVO ;
    }
}
